package com.iwhalecloud.bss.kite.cucc.service.custview;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosGoodsDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosUserDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewAttr;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.GroupUserInfoVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.dataservice.cache.IndustryCache;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.KiteAttrConfigCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.KiteAttrConfig;

/**
 * @ClassName : GetGroupUserInfoTask
 * @Description : 集团用户信息
 * @Author : xie.zhengjun
 * @Date : 2021/1/22 1:40
 * @deprecated 弃用，改用{@link GetUserInfoNeoTask}, 需求稳定后删掉此类
 */
@Deprecated
public class GetGroupUserInfoTask extends CustViewBaseTask {
    public GetGroupUserInfoTask(Object[] args) {
        super(args);
    }

    @Override
    public CustViewBaseVO call() throws Exception {
        Object[] args = getArgs();
        if (args.length != 2) {
            return null;
        }
        KitePosDTO kitePosDTO = (KitePosDTO) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];
        List<KitePosUserDTO> users = kitePosDTO.getUsers();
        GroupUserInfoVO groupUserInfoVO = new GroupUserInfoVO();
        custViewVO.setGroupUser(groupUserInfoVO);
        if (KiteListUtils.isNotEmpty(users)) {
            // 暂时先这样写，按道理来说根据业务号码去查回来的变更单信息也应该只包含一个用户
            KitePosUserDTO kitePosUserDTO = users.get(0);
            List<CustViewAttr> attrs = new ArrayList<>();
            groupUserInfoVO.setAttrs(attrs);
            Optional.ofNullable(kitePosUserDTO.getAttrs()).ifPresent(kitePosAttrDTOs -> {
                // 对报文返回的user节点的attrs进行遍历，结合KiteAttrConfig组装返回给前端的集团用户信息
                kitePosAttrDTOs.forEach(kitePosAttrDTO -> {
                    KiteAttrConfig attrConfig = KiteAttrConfigCache.getAttrConfig(kitePosAttrDTO.getCode(), "-1",
                        getMainCatagoryId(kitePosDTO, kitePosUserDTO.getInstanceId()));
                    if (Objects.isNull(attrConfig)) {
                        attrConfig = KiteAttrConfigCache.getAttrConfig(kitePosAttrDTO.getCode(), "-1", "-1");
                    }
                    if (Objects.nonNull(attrConfig)) {
                        CustViewAttr custViewAttr = new CustViewAttr();
                        custViewAttr.setCode(kitePosAttrDTO.getCode());
                        custViewAttr.setValue(kitePosAttrDTO.getValue());
                        custViewAttr.setValueDesc(kitePosAttrDTO.getValue());
                        if (KiteStringUtils.equalsAny(attrConfig.getAttrType(), KeyConsts.DROPDOWN, KeyConsts.RADIO)) {
                            // 如果是下拉框，需要翻译下拉值
                            custViewAttr.setValueDesc(Optional.ofNullable(
                                AttrValueCache.getAttrValue(attrConfig.getAttrId(), kitePosAttrDTO.getValue()))
                                .map(AttrValue::getAttrValueName).orElse(kitePosAttrDTO.getValue()));
                            // 行业相关
                            if (KiteStringUtils.equalsAny(kitePosAttrDTO.getCode(), LocalKeyConsts.INDUSTRY, LocalKeyConsts.PRODUCT_LINE, LocalKeyConsts.SEGMENT_KEY_PRODUCT)) {
                                custViewAttr.setValueDesc(
                                    Optional.ofNullable(IndustryCache.getIndustryName(kitePosAttrDTO.getValue()))
                                        .orElse(kitePosAttrDTO.getValue()));
                            }
                        }
                        custViewAttr.setName(attrConfig.getAttrName());
                        custViewAttr.setOrderId(attrConfig.getSort());
                        attrs.add(custViewAttr);
                    }
                });

            });
        }
        return groupUserInfoVO;
    }

    private String getMainCatagoryId(KitePosDTO kitePosDTO, String userInstId) {
        return Optional.ofNullable(kitePosDTO.getGoods()).map(kitePosGoodsDTOs -> kitePosGoodsDTOs.stream()
            .filter(kitePosGoodsDTO -> KiteStringUtils.isEqual(kitePosGoodsDTO.getUserInstId(), userInstId)).findFirst()
            .map(KitePosGoodsDTO::getCatagoryId).orElse("")).orElse("");
    }
}
